namespace GoDinner.Migrations
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using System.Web.Security;
    using WebMatrix.WebData;

    internal sealed class Configuration : DbMigrationsConfiguration<GoDinner.Models.GoDinnerDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            this.AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(GoDinner.Models.GoDinnerDbContext context)
        {
            context.UserProfiles.AddOrUpdate<Models.UserProfile>(p => p.UserName,
                    new Models.UserProfile { UserName = "Test" }
                );

            context.GerechtTypes.AddOrUpdate(p => p.Naam,
                    new Models.GerechtType 
                    {
                        Naam="Voorgerechten",
                        Gerechten = new List<Models.Gerecht> 
                        {
                            new Models.Gerecht {Naam = "Soep", Prijs = 4.5, GerechtTypeId=1 },
                            new Models.Gerecht {Naam = "Visreepjes in tartaarsaus", Prijs = 5.5, GerechtTypeId=1 },
                            new Models.Gerecht {Naam = "Tomaten met zelfgemaakte vleessalade", Prijs = 6.5, GerechtTypeId=1 }
                        }                    
                    },

                    new Models.GerechtType
                    {
                        Naam="Hoofdgerechten",
                        Gerechten = new List<Models.Gerecht> 
                        {
                            new Models.Gerecht {Naam = "Lasagne", Prijs = 13.99, GerechtTypeId=2 },
                            new Models.Gerecht {Naam = "Spaghetti", Prijs = 14.99, GerechtTypeId=2 },
                            new Models.Gerecht {Naam = "Zalmfilet met verse groentjes en gemengde sla", Prijs = 15.99, GerechtTypeId=2 },
                            new Models.Gerecht {Naam = "Mosselen in de wok", Prijs = 16.99, GerechtTypeId=2 },
                            new Models.Gerecht {Naam = "Steak natuur", Prijs = 17.99, GerechtTypeId=2 }
                        }
                    },

                    new Models.GerechtType 
                    {
                        Naam="Desserts",
                        Gerechten = new List<Models.Gerecht> 
                        {
                            new Models.Gerecht {Naam = "Dame Blanche", Prijs = 3.99, GerechtTypeId=3 },
                            new Models.Gerecht {Naam = "Pannekoek met Chocolade", Prijs = 4.99, GerechtTypeId=3 },
                            new Models.Gerecht {Naam = "Pannekoek met ijs Blanche", Prijs = 3.99, GerechtTypeId=3 },
                            new Models.Gerecht {Naam = "Pannekoektaart", Prijs = 4.99, GerechtTypeId=3 },
                            new Models.Gerecht {Naam = "Pannekoek met coupe fruit", Prijs = 3.99, GerechtTypeId=3 }
                        }                    
                    },

                    new Models.GerechtType 
                    {
                        Naam="Supplementen",
                        Gerechten = new List<Models.Gerecht> 
                        {
                            new Models.Gerecht {Naam = "Frietjes", Prijs = 2.50, GerechtTypeId=4 },
                            new Models.Gerecht {Naam = "Kroketjes", Prijs = 2.60, GerechtTypeId=4 },
                            new Models.Gerecht {Naam = "Rijst", Prijs = 2.70, GerechtTypeId=4 },
                            new Models.Gerecht {Naam = "Slaatje", Prijs = 2.80, GerechtTypeId=4 }
                        }                    
                    },

                    new Models.GerechtType 
                    {
                        Naam="Snacks",
                        Gerechten = new List<Models.Gerecht> 
                        {
                            new Models.Gerecht {Naam = "Broodje gezond", Prijs = 5.50, GerechtTypeId=5 },
                            new Models.Gerecht {Naam = "Portie bitterballen", Prijs = 5.60, GerechtTypeId=5 },
                            new Models.Gerecht {Naam = "Portie kaas & nootjes", Prijs = 5.70, GerechtTypeId=5 }
                        }                    
                    }
                );

            SeedMembership();

            

            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }

        private void SeedMembership()
        {
            WebSecurity.InitializeDatabaseConnection("DefaultConnection","UserProfile","UserId", "UserName", autoCreateTables:true);

            var roles = (SimpleRoleProvider)Roles.Provider;
            var membership = (SimpleMembershipProvider)Membership.Provider;


            if(!roles.RoleExists("Admin"))
            {
                roles.CreateRole("Admin");
            }
            if (membership.GetUser("admin1", false) == null)
            {
                membership.CreateUserAndAccount("admin1","test123");
            }
            if (!roles.GetRolesForUser("admin1").Contains("Admin"))
            {
                roles.AddUsersToRoles(new[] { "admin1" }, new[] { "Admin" });
            }
            if (!roles.RoleExists("User"))
            {
                roles.CreateRole("User");
            }

        }
    }
}
